package com.ccc.boot.security.service.impl;

import com.ccc.boot.log.ILogService;
import com.ccc.boot.log.LogInfo;
import com.ccc.boot.entity.SysUserOnline;
import com.ccc.boot.monitor.mapper.SysUserOnlineMapper;
import com.ccc.boot.security.service.OnlineService;
import com.ccc.boot.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author superC
 * @version 0.1.0
 * @date 2022/4/25
 */
@Service("onlineService")
public class OnlineServiceImpl implements OnlineService, ILogService {

    @Autowired
    private SysUserOnlineMapper sysUserOnlineMapper;

    /**
     * 删除数据
     *
     * @date 2022/5/1 20:20
     * @param tokenKey
     * @return {@link boolean}
     * @author superC
     */

    @Override
    public boolean delete(String tokenKey) {
        return sysUserOnlineMapper.deleteById(tokenKey) > 0;
    }

    @Override
    public boolean insert(LogInfo info) {
        SysUserOnline online = new SysUserOnline();
        online.setId(info.getTokenkey());
        online.setClientType(info.getClientType());
        online.setUsername(info.getUsername());
        online.setIp(info.getIp());
        online.setAddress(LogUtils.getAddress(info));
        online.setIsp(info.getIsp());
        online.setBrowser(info.getUserAgent());
        online.setLoginTime(LocalDateTime.now());
        return sysUserOnlineMapper.insert(online) > 0;
    }

    @Override
    public void saveLog(LogInfo info) {
        insert(info);
    }
}